package com.cyf.web.service.impl;

import com.cyf.db.bean.*;
import com.cyf.db.mapper.*;
import com.cyf.util.ObjectMapperUtils;
import com.cyf.util.SmsRsp;
import com.cyf.util.SmsUtils;
import com.cyf.web.network.bean.*;
import com.cyf.web.service.WebService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * 处理NPS网站的业务
 * Created by cage on 2017/2/18.
 */
@Service
public class WebServiceImpl implements WebService {

    private static final Logger logger = LoggerFactory.getLogger(WebServiceImpl.class);
    private static final long oneMinute = 60 * 1000;

    @Autowired
    private OnlinePayUserMapper onlinePayUserMapper;
    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private RegSmsMapper regSmsMapper;
    @Autowired
    private AdminparaMapper adminparaMapper;

    @Override
    public WebPayUserRsp registerPayUser(WebPayUserReq req) {
        WebPayUserRsp rsp = new WebPayUserRsp();
        rsp.setResult(WebBusinessRsp.FAIL);
        try {
            Parking parking = parkingMapper.getParkingById(req.getParkingId());
            if (parking == null) {
                rsp.setStrError("车场不存在，激活在线支付失败！");
                return rsp;
            }
//            OnlinePayUser onlinePayUser = onlinePayUserMapper.getByCardNoAndParkingId(req.getCardNo(), req.getParkingId());
            OnlinePayUser onlinePayUser = onlinePayUserMapper.getByParkingId(req.getParkingId());
            if (onlinePayUser != null) {
                if (OnlinePayUser.REVIEW_SUCCESS == onlinePayUser.getStatus()) {
                    rsp.setStrError("已激活在线支付！");
                } else {
                    rsp.setStrError("正在审核激活在线支付信息！");
                }
                return rsp;
            }
            onlinePayUser = new OnlinePayUser();
            BeanUtils.copyProperties(req, onlinePayUser);
            onlinePayUser.setUuid(UUID.randomUUID().toString());
            onlinePayUser.setCreatedTime(new Date());
            onlinePayUserMapper.insertOnlinePayUser(onlinePayUser);
            rsp.setResult(WebBusinessRsp.SUCCESS);
        } catch (Exception e) {
            rsp.setStrError("服务器异常！");
            logger.error("nps网站激活在线支付异常：{}", e);
        }
        return rsp;
    }

    @Override
    public WebSendSmsRsp sendSms(WebSendSmsReq req) {
        WebSendSmsRsp rsp = new WebSendSmsRsp();
        rsp.setResult(WebBusinessRsp.FAIL);
        try {
            Parking parking = parkingMapper.getParkingById(req.getParkingId());
            if (parking == null) {
                rsp.setStrError("车场不存在，发送验证码失败！");
                return rsp;
            }
            RegSms regSms = regSmsMapper.getByMobile(req.getMobile());
            if (regSms != null) {
                long time = new Date().getTime() - regSms.getSmsTime().getTime();
                if (time < oneMinute) {
                    rsp.setStrError("重复提交！");
                    return rsp;
                }
                //webRegSmsMapper.deleteById(webRegSms.getId());
                rsp.setCode(regSms.getCode());
                rsp.setResult(WebBusinessRsp.SUCCESS);
                return rsp;
            }
            Integer code = SmsUtils.getCode();
            regSms = new RegSms();
            regSms.setMobile(req.getMobile());
            regSms.setCode(code);
            regSms.setSmsTime(new Date());

            String txt = SmsUtils.getTxt(code);

            String rspStr = SmsUtils.SendSms(txt, req.getMobile());
            logger.info("nps请求发送短信验证码结果：{}", rspStr);
            SmsRsp smsRsp = ObjectMapperUtils.getObjectMapper().readValue(rspStr, SmsRsp.class);
            if (SmsRsp.SUCCESS == smsRsp.getCode()) {
                regSmsMapper.insertRegSms(regSms);
                rsp.setCode(code);
                rsp.setResult(WebBusinessRsp.SUCCESS);
            } else {
                rsp.setStrError("发送验证码失败！");
            }
        } catch (Exception e) {
            rsp.setStrError("服务器异常！");
            logger.error("nps网站发送短信验证码异常：{}", e);
        }
        return rsp;
    }

    @Override
    public WebGetReviewStatusRsp getReviewStatus(WebGetReviewStatusReq req) {
        WebGetReviewStatusRsp rsp = new WebGetReviewStatusRsp();
        rsp.setResult(WebBusinessRsp.FAIL);
        try {
            Parking parking = parkingMapper.getParkingById(req.getParkingId());
            if (parking == null) {
                rsp.setStrError("车场不存在，查询审核状态失败！");
                return rsp;
            }
            OnlinePayUser onlinePayUser = onlinePayUserMapper.getByParkingId(req.getParkingId());
            if (onlinePayUser == null) {
                rsp.setStrError("未查询到车场的支付激活信息！");
                return rsp;
            }
            rsp.setResult(WebBusinessRsp.SUCCESS);
            rsp.setStatus(onlinePayUser.getStatus());
        } catch (Exception e) {
            rsp.setStrError("服务器异常！");
            logger.error("nps网站查询审核状态异常：{}", e);
        }
        return rsp;
    }

    @Override
    public WebGetInfoRsp getInfo(WebGetInfoReq req) {
        WebGetInfoRsp rsp = new WebGetInfoRsp();
        rsp.setResult(WebBusinessRsp.FAIL);
        try {
            Parking parking = parkingMapper.getParkingById(req.getParkingId());
            if (parking == null) {
                rsp.setStrError("车场不存在，查询信息失败！");
                return rsp;
            }
            OnlinePayUser onlinePayUser = onlinePayUserMapper.getByParkingId(req.getParkingId());
            if (onlinePayUser == null) {
                rsp.setStrError("未找到激活在线支付信息！");
                return rsp;
            }
            BeanUtils.copyProperties(onlinePayUser, rsp);
            rsp.setResult(WebBusinessRsp.SUCCESS);
        } catch (Exception e) {
            rsp.setStrError("服务器异常！");
            logger.error("nps网站查询审核状态异常：{}", e);
        }
        return rsp;
    }

    @Override
    public Adminpara findParaValueByParkingId(Integer parkingId) {
        return adminparaMapper.getParaValueByParkingId(parkingId,Adminpara.LICENSE_INITIALS);
    }
}
